package com.atguigu.gulimall.ssoclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhenjun
 * @date 2022/3/17 10:09
 */
@Controller
public class HelloController {

    @Value("${sso.server.url}")
    String ssoServerUrl;
    /**
     * 无需登录就可访问
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    /**
     * 登录验证
     * @param model
     * @param session
     * @param token
     * @return
     */
    @GetMapping("employees")
    public String employees(Model model, HttpSession session,@RequestParam(value = "token",required = false) String token){
        if (!StringUtils.isEmpty(token)) {
            // TODO 去获取真实的用户信息
            session.setAttribute("loginUser","zhangsan");
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
            // 没有登录，去登录
            return "redirect:" + ssoServerUrl + "?redirect_url=http://client1.com:8081/employees";
        }
        List<String> result = new ArrayList<>();
        result.add("张三");
        result.add("李四");
        model.addAttribute("emps",result);
        return "list";
    }

}
